// 小鹿

(function (global) {
  function deer() {}
  function isWindow(obj) {
      return obj != null && obj === obj.window;
  }
  function getWindow(elem) {
    return isWindow(elem) ? elem : elem.nodeType === 9 && elem.defaultView;
  }
  deer.prototype.scroll = function (target) {};
  deer.prototype.scroll = function (callback) {
    window.addEventListener("scroll", scrollEvent, false);
  };
  deer.prototype.get = function (id) {
    return document.getElementById(id);
  };
  deer.prototype.height = function(id) {
      let elem = this.get(id);
      return elem.getBoundingClientRect().height;
  };
  deer.prototype.offset = function (id) {
    var docElem,
      win,
      elem = this.get(id),
      box = { top: 0, left: 0 },
      doc = elem && elem.ownerDocument; //当前文档

    if (!doc) {
      return;
    }
    docElem = doc.documentElement;
    win = getWindow(doc); //如果是window就返回window，如果是document，返回document.defaultView
    return {
      top: box.top + win.pageYOffset - docElem.clientTop,
      left: box.left + win.pageXOffset - docElem.clientLeft,
    };
  };
  global.DEER = new deer;
})(window);

let startOpen = 0;
const scrollEvent = () => {
  const scrollTop = document.getElementsByTagName("html")[0].scrollTop;
  const setAsset = function(img) {
      DEER.get('pic1').src = `./images/${img}.jpg`;
  }
  console.log(scrollTop, startOpen, startOpen + 400)
  if (scrollTop > startOpen && scrollTop < startOpen + 400) {
      console.log('?')
    let offset = Math.floor(((scrollTop - startOpen) / 400) * 120);
    let newAsset = "";
    if (offset < 1) {
      offset = 1;
    } else if (offset > 120) {
      offset = 120;
    }
    if (offset < 10) {
      newAsset = `large_000${offset}`;
    } else if (offset < 100) {
      newAsset = `large_00${offset}`;
    } else {
      newAsset = `large_0${offset}`;
    }
    
      setAsset(newAsset);
  }

  if (scrollTop < startOpen) {
      setAsset("large_0001");
  }

  if (scrollTop > startOpen + 400) {
      setAsset("large_0120");
  }
};

console.log(DEER.offset("imgWrapper").top,
window.innerHeight / 2,
DEER.height('imgWrapper') / 2
)
startOpen = 1;
//   DEER.offset("imgWrapper").top -
//   (window.innerHeight / 2 - DEER.height('imgWrapper') / 2);
DEER.scroll(scrollEvent, startOpen);
scrollEvent(startOpen);
// scroll 绑定
// 基础选择器
// 动画方法
// css动效
//
// window.addEventListener('scroll', scrollEvent, false);
// scrollEvent();
// return ()=>{
//     window.removeEventListener('scroll', scrollEvent, false);
//   }
